<?php

namespace App\Helpers;

use CodeIgniter\HTTP\Files\UploadedFile;

class NewsHelper
{
    /**
     * 新增
     * @param int $userId
     * @param string $title
     * @param string $summary
     * @param string $url
     * @param UploadedFile|NULL $cover
     * @return void
     * @throws \Exception
     */
    public function create(int $userId, string $title, string $summary = "", string $url = "", UploadedFile $cover = NULL)
    {
        $path = NULL;

        if ($cover instanceof UploadedFile && $cover->isValid()) {
            $path = CommonUtil::getUploadFilePath($cover, 'cover');
        }

        db_connect()->table("news")->insert([
            "title"       => trim($title),
            "summary"     => trim($summary),
            "url"         => trim($url),
            "cover"       => $path,
            "user_id"     => $userId,
            "create_time" => date("Y-m-d H:i:s"),
            "modify_time" => date("Y-m-d H:i:s")
        ]);
    }

    /**
     * 修改
     * @param int $userId
     * @param $id
     * @param $title
     * @param string $summary
     * @param string $url
     * @param UploadedFile|NULL $cover
     * @return void
     * @throws \Exception
     */
    public function modify(int $userId, $id, $title, string $summary = "", string $url = "", UploadedFile $cover = NULL)
    {
        $news = db_connect()->table("news")->where("id", $id)->get()->getRow();

        if (empty($news) || !empty($news->delete_time)) {
            throw new \Exception("文章不存或已删除");
        }

        if (!empty($news->publish_time)) {
            throw new \Exception("文章已发布");
        }

        if ($news->user_id != $userId) {
            throw new \Exception("只能修改自己的文章");
        }

        $data = [
            "title"       => trim($title),
            "summary"     => trim($summary),
            "url"         => trim($url),
            "modify_time" => date("Y-m-d H:i:s"),
        ];

        if ($cover instanceof UploadedFile && $cover->isValid()) {
            $data['cover'] = CommonUtil::getUploadFilePath($cover, 'cover');
        }

        db_connect()->table("news")->where("id", $id)->update($data);
    }

    /**
     * 发布
     * @throws \Exception
     */
    public function publish(int $userId, int $id, string $publishTime = NULL)
    {
        $news = db_connect()->table("news")->where("id", $id)->get()->getRow();

        if (empty($news) || !empty($news->publish_time) || !empty($news->delete_time)) {
            throw new \Exception("文章不存在或已发布");
        }

        if ($news->user_id != $userId) {
            throw new \Exception("只能发布自己创建的文章");
        }

        if (empty($publishTime)) $publishTime = date("Y-m-d H:i");

        db_connect()->table("news")->where("id", $id)->update([
            "publish_time" => $publishTime,
        ]);
    }

    /**
     * 撤回
     * @param int $userId
     * @param int $id
     * @return void
     * @throws \Exception
     */
    public function recall(int $userId, int $id)
    {
        $news = db_connect()->table("news")->where("id", $id)->get()->getRow();

        if (empty($news) || empty($news->publish_time) || !empty($news->delete_time)) {
            throw new \Exception("文章不存在或未发布");
        }

        if ($news->user_id != $userId) {
            throw new \Exception("只能撤回自己发布的文章");
        }

        db_connect()->table("news")->where("id", $id)->update(["publish_time" => NULL]);
    }

    /**
     * 删除
     * @throws \Exception
     */
    public function delete(int $userId, int $id)
    {
        $news = db_connect()->table("news")->where("id", $id)->get()->getRow();

        if (empty($news) || !empty($news->delete_time)) {
            throw new \Exception("文章不存在或已删除");
        }

        if ($news->user_id != $userId) {
            throw new \Exception("只能删除自己的文章");
        }

        $db = db_connect();
        $db->table("news")->where("id", $id)->update([
            "delete_time" => date("Y-m-d H:i:s"),
        ]);
    }

    /**
     * 恢复
     * @throws \Exception
     */
    public function recovery(int $userId, int $id)
    {
        $news = db_connect()->table("news")->where("id", $id)->get()->getRow();

        if (empty($news) || empty($news->delete_time)) {
            throw new \Exception("文章不存在");
        }

        if ($news->user_id != $userId) {
            throw new \Exception("只能恢复自己的文章");
        }

        $db = db_connect();
        $db->table("news")->where("id", $id)->update(["delete_time" => NULL]);
    }
}